/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.cslg.news.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.cslg.news.service.UserService;

/**
 *
 * @author jimshen
 */
@WebServlet("/username_available")
public class UserExists extends HttpServlet {

	private static final long serialVersionUID = 3076226027605259937L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String username = req.getParameter("username");
		UserService us = new UserService();
		try {
			boolean res = us.isUserExists(username);
			try (PrintWriter out = resp.getWriter()) {
				if (res)
					out.println("0");
				else
					out.println("1");
			}
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}
}
